<template>
  <div class="tags" v-if="showTags">
    <ul>
      <li
        class="tags-li"
        v-for="(item, index) in tagsList"
        @click="getrouter(item, index)"
        :class="{ active: isActive(item.path) }"
        :key="index"
      >
        <router-link :to="item.path" class="tags-li-title">
          {{ item.title }}
        </router-link>
        <span class="tags-li-icon" @click="closeTags(index)"
          ><i class="el-icon-close"></i
        ></span>
      </li>
    </ul>
    <!-- <div class="tags-close-box">
			<el-dropdown @command="handleTags">
				<el-button size="mini" type="info" plain>
					标签选项<i class="el-icon-arrow-down el-icon--right"></i>
				</el-button>
				<el-dropdown-menu size="small" slot="dropdown">
					<el-dropdown-item command="other">关闭其他</el-dropdown-item>
					<el-dropdown-item command="all">关闭所有</el-dropdown-item>
				</el-dropdown-menu>
			</el-dropdown>
		</div> -->
  </div>
</template>

<script>
import bus from "@/common/bus";
export default {
  data() {
    return {
      tagsList: [],
      tagsindex: "",
    };
  },
  methods: {
    getrouter(row, index) {
      console.log(row);
      this.tagsindex = index;
      // console.log(row);
      // this.$router.push(row.path)
    },
    isActive(path) {
      return path === this.$route.fullPath;
    },
    // 关闭单个标签
    closeTags(index) {
      const delItem = this.tagsList.splice(index, 1)[0];
      if (delItem != undefined) {
        this.$router.push(delItem.path);
      }
      const item = this.tagsList[index]
        ? this.tagsList[index]
        : this.tagsList[index - 1];
      if (item != undefined) {
        delItem.path === this.$route.fullPath && this.$router.push(item.path);
      } else {
        // this.$router.push("/main");
        this.$router.push("/");
      }
    },
    // 关闭全部标签
    closeAll() {
      this.tagsList = [];
      //                this.$router.push('/home');
    },
    // 关闭其他标签
    closeOther() {
      const curItem = this.tagsList.filter((item) => {
        return item.path === this.$route.fullPath;
      });
      this.tagsList = curItem;
    },
    // 设置标签
    setTags(route) {
      // console.log('添加tags')
      const isExist = this.tagsList.some((item) => {
        return item.path === route.fullPath;
      });
      if (!isExist) {
        if (this.tagsList.length >= 8) {
          this.tagsList.shift();
        }
        // console.log('添加tags')
        // console.log('route', route)
        this.tagsList.push({
          title: route.meta.title,
          path: route.fullPath,
          name: route.name
        });
      }
      bus.$emit("tags", this.tagsList);
      // console.log('tagsList', this.tagsList)
    },
    handleTags(command) {
      command === "other" ? this.closeOther() : this.closeAll();
    },
  },
  computed: {
    showTags() {
      return this.tagsList.length > 0;
    },
  },
  watch: {
    $route(newValue, oldValue) {
      for (let i = 0; i < this.tagsList.length; i++) {
        if (newValue.meta.title == this.tagsList[i].title) {
          this.tagsList[i].path = newValue.fullPath;
        }
      }
      if (newValue.meta.tags != undefined) {
      } else {
        this.setTags(newValue);
      }
    },
  },
  created() {
    this.setTags(this.$route);
    // 监听关闭当前页面的标签页
    bus.$on("close_current_tags", () => {
      for (let i = 0, len = this.tagsList.length; i < len; i++) {
        if (item.path === this.$route.fullPath) {
          if (i < len - 1) {
            this.$router.push(this.tagsList[i + 1].path);
          } else if (i > 0) {
            this.$router.push(this.tagsList[i - 1].path);
          } else {
          }
          this.tagsList.splice(i, 1);
          
        }
      }
    });
  },
  mounted() {
  },
};
</script>


<style lang="less">
.tags-li-title {
  color: #000000;
}
.tagsactive {
  background: #fff !important;
}
.tags {
  position: relative;
  height: 32px;
  overflow: hidden;
  background: #f0f2f6;
  /* left: 160px; */
  /* width: calc(100% - 160px); */
}

.tags ul {
  position: relative;
  box-sizing: border-box;
  height: 100%;

  &:after {
    content: '';
    position: absolute;
    width: 100%;
    height: 1px;
    left: 0;
    bottom: 0;
    background: #E6EAF1;
  }
}

.tags-li {
  position: relative;
  z-index: 3;
  float: left;
  margin: 3px 4px 0px 0px;
  border-radius: 3px;
  font-size: 12px;
  // overflow: hidden;
  cursor: pointer;
  height: 28px;
  line-height: 28px;
  border: 1px solid;
  border-color: #E6EAF1 #E6EAF1 #fff #E6EAF1;
  background: #f6f7f9;
  padding: 0 12px 0 12px;
  vertical-align: middle;
  color: #092a4a;
  transition: all 0.3s ease-in;
  width: 144px;
  display: flex;
  justify-content: space-between;
}
.active {
  background: #fff;
}
.tags-li:not(.active):hover {
  /* background: #d9d9d9; */
}

.tags-li-title {
  float: left;
  width: 144px;
  /* max-width: 80px; */
  // overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  margin-right: 5px;
  color: #666;
  text-decoration: none;
}

.tags-close-box {
  position: absolute;
  right: 0;
  top: 0;
  /* bottom: 200px; */
  box-sizing: border-box;
  padding-top: 1px;
  text-align: center;
  width: 110px;
  height: 30px;
  background: #fff;
  z-index: 10;
}
</style>
